package com.taomu.app.zongchouhuli.moudle.home.contract.impl;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResMainProductListEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductDetailEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductDetailInfoEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.moudle.home.contract.HomeProductDetailContract;
import com.taomu.app.zongchouhuli.moudle.home.entity.ProductHistEntity;
import com.taomu.app.zongchouhuli.moudle.index.entity.ProductEntity;
import com.taomu.app.zongchouhuli.utils.ParseUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class HomeProductDetailPresenter implements HomeProductDetailContract.Presenter {

    private HomeProductDetailContract.View mContractView;

    private CompositeDisposable disposable;

    public HomeProductDetailPresenter(HomeProductDetailContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getBaseProductInfo(String _id) {
        Request.getIntence().getProductDetailInfo(_id)
                .flatMap(new Function<String, Observable<BaseChangeResponse<ResProductDetailInfoEntity>>>() {
                    @Override
                    public  Observable<BaseChangeResponse<ResProductDetailInfoEntity>> apply(String json) throws Exception {
                        BaseResponse<JSONObject> baseResponse = ParseUtil.parseObject(json,BaseResponse.class);
                        BaseChangeResponse<ResProductDetailInfoEntity> data = new BaseChangeResponse<>();
                        data.setData(ParseUtil.parseObject(baseResponse.getData().toString(),ResProductDetailInfoEntity.class));
                        data.setCount(baseResponse.getCount());
                        return Observable.just(data);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseChangeResponse<ResProductDetailInfoEntity>>(mContractView,disposable,true) {
                    @Override
                    public void onNext(BaseChangeResponse<ResProductDetailInfoEntity> data) {
                        if(mContractView != null){
                            mContractView.setBaseInfo(data);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        Log.d("wqdqwdwqdwqdwq","onError:"+e.getMessage());
                    }
                });
    }

    @Override
    public void getSpecialListData() {
        setSpecialList();
    }

    @Override
    public void getBuyHistListData() {
        setProductHis();
    }

    @Override
    public void getProductListData() {
        setProductList();
    }

    @Override
    public void getProductRecommond() {
        Request.getIntence().getMainPageDataList("is_recommend")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResMainProductListEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResMainProductListEntity resProductEntities) {
                        if(mContractView != null){
                            mContractView.setRecommond(resProductEntities);
                        }
                    }
                });
    }

    @Override
    public void getShare(String productid, String userid) {
        Request.getIntence().getShareUrl(productid, userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String url) {
                        if(mContractView != null){
                            mContractView.setShareUrl(url);
                        }
                    }
                });
    }

    @Override
    public void detch() {

    }

    private void setProductList(){
        List<ProductEntity> entities = new ArrayList<>();
        for(int i=0;i<4;i++){
            ProductEntity entity = new ProductEntity();
            entity.setTitle("产品名称:::::"+i);
            entity.setLimitNum("");
            entity.setPrice("¥666");
            entity.setIncome("最高收益3.25%");
            entities.add(entity);
        }
        if(mContractView != null){
            mContractView.setProductListData(entities);
        }
    }

    private void setProductHis(){
        List<ProductHistEntity> entities = new ArrayList<>();
        for(int i=0;i<4;i++){
            ProductHistEntity entity = new ProductHistEntity();
            entity.setContent(String.valueOf(i+i)+".用户张**购买了本产品");
            entity.setTime("2018-08-30 15:26");
            entities.add(entity);
        }
        if(mContractView != null){
            mContractView.setBuyHistListData(entities);
        }
    }

    private void setSpecialList(){
        List<String> entities = new ArrayList<>();
        for(int i=0;i<4;i++){
            if(i == 0){
                entities.add("产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍产品特色介绍");
            }else{
                entities.add("产品特色介绍产品特色介绍");
            }
        }
        if(mContractView != null){
            mContractView.setSpecialListData(entities);
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        disposable = null;
    }
}
